using Unity.VisualScripting;
using UnityEngine;

public class Slingshot : MonoBehaviour
{
    public static Slingshot Instance;
    public LineRenderer leftLineRenderer;
    public LineRenderer rightLineRenderer;
    public Transform leftPoint;
    public Transform rightPoint;
    public Transform shootPoint;
    private bool isDrawing = false;//是否画线
    private Transform birdTransform;//鸟

    private void Awake()
    {
        Instance = this;
        HideLine();
    }

    private void Update()
    {
        if (isDrawing)
        {
            Draw();
        }
    }

    public void StartDraw(Transform birdTransform)
    {

        isDrawing = true;
        this.birdTransform = birdTransform;
        ShowLine();
    }

    public void EndDraw()
    {
        isDrawing = false;
        HideLine();
    }
    public void Draw()
    {
        Vector2 birdPosition = birdTransform.position + birdTransform.GetComponent<CircleCollider2D>().radius * (birdTransform.position - transform.position).normalized;
        leftLineRenderer.SetPosition(0, birdPosition);
        leftLineRenderer.SetPosition(1, leftPoint.position);
        rightLineRenderer.SetPosition(0, birdPosition);
        rightLineRenderer.SetPosition(1, rightPoint.position);
    }

    void HideLine()
    {
        leftLineRenderer.enabled = false;
        rightLineRenderer.enabled = false;
    }

    void ShowLine()
    {
        leftLineRenderer.enabled = true;
        rightLineRenderer.enabled = true;
    }
}
